package com.dingwei.superiordiary.mvp.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ab.util.AbFileUtil;
import com.ab.util.AbStrUtil;
import com.ab.util.AbToastUtil;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.PersonalBean;
import com.dingwei.superiordiary.bean.Upload_Head_Image_Bean;
import com.dingwei.superiordiary.bean.UserDataBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.CropImageActivity;
import com.dingwei.superiordiary.ui.activity.PersonalDataActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.utils.UrlUtils;
import com.lihao.baselibrary.weight.WheelView;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihao on 2017/9/12.
 */

public class PersonalDataPresenter extends basePresenter {

    private Contract.PersonalDataView personalDataView;


    private Dialog sexDialog;//性别弹出框

    public PersonalDataPresenter(Contract.PersonalDataView view) {
        super(view);
        this.personalDataView = view;
    }


    public void getUserData() {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.correlation);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, PersonalBean.class,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                PersonalBean bean = (PersonalBean) baseBean;

                BaseActivity.value.setUserName(bean.data.nickname);
                BaseActivity.value.setHeadImg(bean.data.portrait);
                personalDataView.setUserData(bean);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                personalDataView.Fail();
            }
        });
    }

    /**
     * 上传头像
     *
     * @param lastPath
     */
    public void uploadHeadImg(String lastPath) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.UPLOAD_HEAD_IMAGE);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("portrait", new File(lastPath));
        x.http().post(rp, new myCallback(BaseActivity.context, Upload_Head_Image_Bean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                Upload_Head_Image_Bean bean = (Upload_Head_Image_Bean) baseBean;
                BaseActivity.value.setHeadImg(bean.data);
                personalDataView.uploadPicSuccess(bean);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }


    /**
     * 性别选择框
     *
     * @param activity
     * @param sex      目前的性别
     */
    public void showChooseSexDialog(PersonalDataActivity activity, final String sex) {

        List<String> list = new ArrayList<>();
        list.add("男");
        list.add("女");
        list.add("保密");
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_check_sex, null);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tvSubmit = (TextView) view.findViewById(R.id.tv_submit);
        final WheelView wheelView = (WheelView) view.findViewById(R.id.wheel);
        wheelView.setOffset(2);
        wheelView.setItems(list);
        view.measure(0, 0);
        if (sex.equals("男")) {
            wheelView.setSeletion(0);
        } else if (sex.equals("女")) {
            wheelView.setSeletion(1);
        } else {
            wheelView.setSeletion(2);
        }
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sexDialog.dismiss();
            }
        });
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sexDialog.dismiss();

                if (!wheelView.getSeletedItem().equals(sex)) {
                    personalDataView.ChooseSex(wheelView.getSeletedItem());
                } else {
                    showToast("保存成功");
                }
            }
        });
        sexDialog = new Dialog(activity, R.style.Theme_Light_Dialog);
        sexDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        sexDialog.setContentView(view);
        Window dialogWindow = sexDialog.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        sexDialog.show();
    }

    /**
     * 保存用户性别
     *
     * @param gender
     */
    public void saveGender(final String gender) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.EDIT_USER_DATA);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        switch (gender) {
            case "保密":
                rp.addBodyParameter("gender", "1");
                break;
            case "男":
                rp.addBodyParameter("gender", "2");
                break;
            case "女":
                rp.addBodyParameter("gender", "3");
                break;

        }
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                personalDataView.saveGenderSuccess(gender);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

//    public void WXlogin(final String type, final String uid) {
//        final RequestParams rp = new RequestParams(URLS.HOST + URLS.EDIT_USER_DATA);
//        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
//        switch (type) {
//            case "QQ":
//                rp.addBodyParameter("bind_qq",uid);
//                break;
//            case "WEIXIN":
//                rp.addBodyParameter("bind_wx",uid);
//                break;
//        }
//        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
//            @Override
//            public void back(MyBaseBean baseBean, String result) {
//                switch (type) {
//                    case "QQ":
//                        if (TextUtils.isEmpty(uid)){
//                            personalDataView.unbindQQSuccess();
//                        }else {
//                            personalDataView.bindQQSuccess();
//                        }
//                        break;
//                    case "WEIXIN":
//                        if (TextUtils.isEmpty(uid)){
//                            personalDataView.unbindWXSuccess();
//                        }else {
//                            personalDataView.bindWXSuccess();
//                        }
//
//                        break;
//                }
//            }
//
//            @Override
//            public void fail(FailsBean baseBean, String result) {
//            }
//        });
//    }
}
